﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;
namespace WindowsGame4
{
    public class RenderManager
    {
        public List<GameObject> objects;
        public int width, height;
        public SpriteBatch batch;
        public GraphicsDeviceManager graphics;
        public RenderManager(GraphicsDeviceManager graphics, List<GameObject> objects, int width, int height)
        {
            this.graphics = graphics;
            this.objects = objects;
            this.width = width;
            this.height = height;
            batch = new SpriteBatch(graphics.GraphicsDevice);
        }
        public void Draw()
        {
            graphics.GraphicsDevice.Clear(Color.Black);
            batch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);
            foreach (GameObject obj in objects)
                if (obj.bVisible)
                   obj.Draw(batch);
            DrawHUD();
            batch.Draw(GameManager.gameOver, new Vector2(), new Color(GameManager.goColor));
            batch.End();
            if (GameManager.player.bDead)
                GameManager.goColor.W += 1.0f / 120.0f;
        }
        public void DrawHUD()
        {
            string str;
            Vector2 pos;
            Vector4 color;
            str = "Minerals: "+GameManager.player.minerals;
            pos = new Vector2(0, 0);
            color = new Vector4(1, 1, 1, 1);
            batch.DrawString(GameManager.basicFont, str, pos, new Color(color));
            str = "Specimens: " + GameManager.player.specimens;
            pos.Y += 16;
            color = new Vector4(1, 1, 1, 1);
            batch.DrawString(GameManager.basicFont, str, pos, new Color(color));
            str = "Objects: " + GameManager.nObjects;
            pos.Y += 16;
            color = new Vector4(1, 1, 1, 1);
            batch.DrawString(GameManager.basicFont, str, pos, new Color(color));
            DrawHealthBar();
        }
        public Vector4 GetMediumColor(Vector4 c1, Vector4 c2, float secondPortion, bool bumpLuminance = false)
        {
            c1 *= (1 - secondPortion);
            c2 *= secondPortion;
            Vector4 color = c1 + c2;
            if (bumpLuminance)
                color = BumpLuminance(color);
            return color;
        }
        public void DrawHealthBar()
        {
            float portion = GameManager.player.curHealth / GameManager.player.maxHealth;
            Vector4 barColor = GetMediumColor(new Vector4(1, 0, 0, 1), new Vector4(0, 1, 0, 1), portion);
            DrawBar(new Vector2(200, 0), new Vector2(400, 15), portion, barColor);
        }
        public Vector4 BumpLuminance(Vector4 color)
        {
            float invLum = 1.0f - Math.Max(Math.Max(color.X, color.Y), color.Z);
            if (color.X > 0.0f) color.X += invLum;
            if (color.Y > 0.0f) color.Y += invLum;
            if (color.Z > 0.0f) color.Z += invLum;
            return color;
        }
        public void DrawBar(Vector2 pos, Vector2 size, float fillPortion, Vector4 color)
        {
            GameObject obj = new GameObject();
            obj.texture = GameManager.pixel;
            //draw white
            obj.color = new Vector4(1, 1, 1, 1);
            obj.scale = size;
            obj.pos = GameObject.GetGlobal(pos);
            obj.depth = 1;
            obj.Draw(batch);
            //draw black
            obj.color = new Vector4(0, 0, 0, 1);
            obj.scale.X -= 2;
            obj.scale.Y -= 2;
            obj.pos += new Vector2(1, 1);
            obj.depth = .5f;
            obj.Draw(batch);
            //draw the fill
            obj.pos += new Vector2(1, 1);
            obj.scale.X -= 2;
            obj.scale.Y -= 2;
            obj.color = color;
            obj.scale.X *= fillPortion;
            obj.depth=0;
            obj.Draw(batch);
        }
    }
}
